import { Dialog } from 'vant';
import { Toast } from "vant";
import http from "@/common/request";
export default {
    namespaced: true,
    state: {
        goodsInfo: [],
        selectList: []
    },
    getters: {
        isCheckedAll(state) {
            return state.goodsInfo.length == state.selectList.length
        },
        total(state) {
            let total = {
                num: 0,
                price: 0
            }

            state.goodsInfo.forEach((v) => {
                if (v.checked) {
                    total.num += (v.goods_num - 0);
                    total.price += v.goods_num * v.goods_price;
                }

            }
            )
            return total
        }
    },
    mutations: {
        addGoods(state, goods) {
            let arr = []
            // console.log(goods);
            // goods.forEach(v => {
            //     state.selectList.push(v.id)
            // })
            state.goodsInfo = goods
            // state.selectList = goods.id
            goods.forEach(v=>arr.push(v.id))
            state.selectList = arr;
            // console.log(state);
        },
        // 全选
        allChecked(state) {
            state.selectList = state.goodsInfo.map(v => {
                v.checked = true;
                return v.id;
            })
            console.log(state.goodsInfo);
        },
        // 全不选
        unAllChecked(state) {
            state.goodsInfo.forEach(v => v.checked = false)
            state.selectList = []
            console.log(state.goodsInfo);
        },
        // 单选
        checkItem(state, index) {
            // console.log(index);
            let id = state.goodsInfo[index].id;
            let i = state.selectList.indexOf(id);
            //能在selectList中找到对应的id
            if (i != -1) {
                state.selectList.splice(i, 1)
            } else {
                state.selectList.push(state.goodsInfo[index].id)
            }
        }
    },
    actions: {
        checkAllFn({ commit, getters }) {
            getters.isCheckedAll ? commit('unAllChecked') : commit('allChecked')
        },
        deleteGoods({ state },id) {
            // console.log(state.selectList);
            if (state.selectList.length == 0) {
                Toast("请选择商品");
            }
            let arrId = []
            // console.log(111);
            Dialog.confirm({

                message: '是否删除该商品',
            })
                .then(async() => {
                    // on confirm
                    // console.log(111);
                    if (typeof id == 'number') {
                        arrId = [id]
                    } 
                    else {
                        arrId = state.selectList
                    }
                    // console.log(arrId);
                    let res = await http.$axios({
                        url:'/api/deleteCart',
                        method:'post',
                        data:{
                            arrId
                        }
                    })
                    if(res.success){
                        if (typeof id == 'number') {
                          let index = state.goodsInfo.findIndex((v)=>{return v.id = id})
                           state.goodsInfo.splice(index,1)
                           Toast("删除成功");
                        } 
                        else {
                            state.goodsInfo = []
                            Toast("删除成功");
                        }  

                    }

                    console.log(res);
                })
                .catch(() => {
                    // on cancel
                    Toast("取消");
                });
        }
    }
}